<!--车间用电-->
<template>
  <div class="workshop">
    <div class="operate card">
      <!-- <div>时间维度:</div> -->
      <div>
        <el-select style="width:70px" v-model="t_value" @change="t_change">
          <el-option v-for="item in options_t_type" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </div>
      <!-- <div>时间区间:</div> -->
      <div>
        <el-date-picker style="width:150px" v-model="s_time" :editable="false" :clearable="false" :type="d_type" :format="v_type" :value-format="v_type" :picker-options="pickerOptions" placeholder="选择日期时间"></el-date-picker>
      </div>
      <div class="hidden-xs-only">
        <i class="el-icon-minus"></i>
      </div>
      <div>
        <el-tooltip :content="content" placement="top">、
          <el-date-picker style="width:150px" v-model="e_time" :editable="false" :clearable="false" :type="d_type" :format="v_type" :value-format="v_type" :picker-options="pickerOptions" placeholder="选择日期时间"></el-date-picker>
        </el-tooltip>
      </div>
      <!-- <div>选择车间:</div> -->
      <div>
        <el-select style="width:150px" v-model="Workshop_value" @change="change_Workshop">
          <el-option v-for="item in Workshop" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </div>
      <!-- <div>选择生产线:</div> -->
      <div>
        <el-select style="width:150px" v-model="ProductionLine_value">
          <el-option v-for="item in ProductionLine" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </div>
      <div>
        <el-button type="primary" :disabled="!ProductionLine_value&&!Workshop_value" @click="t_button">查询</el-button>
      </div>
      <div class="export">
        <el-tooltip content="导出" :disabled="!ProductionLine_value&&!Workshop_value" placement="top">
          <el-button type :disabled="!ProductionLine_value&&!Workshop_value" @click="t_export">
            <i style="color:#0780EDfont-size:20px" class="iconfont icon-daochu"></i>
          </el-button>
        </el-tooltip>
      </div>
    </div>
    <el-row class="card" :gutter="0">
      <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        <Mtitle title="用电曲线"></Mtitle>
        <ecline v-loading="ecline_loading" v-no-data="ecline_no_data" style="height:400px" unit="kWh" :ecdata="ec01_data" width="100%" height="100%"></ecline>
      </el-col>
    </el-row>
    <el-row class="card" :gutter="0">
      <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        <Mtitle title="车间用电详情"></Mtitle>
        <tb tableTitle="tb_003" :tableData="tableData" v-loading="table_loading"></tb>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import day from "@/utils/day.js"
import Mtitle from '@/components/Mtitle'
import ecline from '@/components/echarts/ec_line'
import tb from '@/components/table'
export default {
  components: {
    Mtitle,
    tb,
    ecline
  },
  watch: {
  },
  data() {
    return {
      content: '查询时间跨度最大为31天',
      ecline_no_data: false,
      export_ProductionLine_value: '',
      ecline_loading: true,
      ec01_data: [

      ],
      Workshop: [],
      Workshop_value: "",
      ProductionLine_Workshop_value: 1,
      table_loading: true,
      ProductionLine: [],
      ProductionLine_value: '',
      tableData: [],
      d_type: "date",
      s_time: "",
      e_time: "",
      v_type: "yyyy-MM-dd",
      // 日月年
      t_value: 2,
      options_t_type: [
        // {
        //   value: 1,
        //   label: "日"
        // },
        {
          value: 2,
          label: "月"
        },
        {
          value: 3,
          label: "年"
        }
      ],
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      export_s_time: '',
      export_e_time: '',
      export_value: ''
    }
  },
  watch: {
    ProductionLine_Workshop_value(a, b) {
      if (a === 3) {
        // 车间用电统计表格
        this.getExcel()
        // 车间用电统计查询曲线
        this.getElectricity()
      }
    }
  },
  mounted() {
  },
  created() {
    // 初始化默认时间
    this.s_e = day.init_value(this.d_type)
    this.s_time = this.s_e.s
    this.e_time = this.s_e.e
    // 获取车间下拉列表
    this.export_s_time = this.s_time
    this.export_e_time = this.e_time
    this.export_value = this.t_value
    this.getWorkshop()
  },
  methods: {
    // 车间用电统计表格
    getExcel() {
      this.$axios
        .post(this.Eelec + "/workshop/getExcel", {
          downsample: this.t_value === 2 ? "month" : "year",
          equipID: null,
          id: this.ProductionLine_value,
          startTime: this.s_time,
          endTime: this.e_time
        })
        .then(res => {
          this.table_loading = false
          if (res.data.code === 200) {
            this.tableData = res.data.data
          }
        })
    },
    // 车间用电统计查询曲线
    getElectricity() {
      this.$axios
        .post(this.Eelec + "/workshop/getElectricity", {
          downsample: this.t_value === 2 ? "month" : "year",
          equipID: null,
          id: this.ProductionLine_value,
          startTime: this.s_time,
          endTime: this.e_time
        })
        .then(res => {
          this.ecline_loading = false
          if (res.data.code === 200) {
            this.ecline_no_data = false
            this.ec01_data = [
              {                name: '总电量',
                value: res.data.data.sumList
              },
              {                name: '尖',
                value: res.data.data.tipList
              },
              {                name: '峰',
                value: res.data.data.peakList
              },
              {                name: '平',
                value: res.data.data.flatList
              },
              {                name: '谷',
                value: res.data.data.valleyList
              },
            ]

          } else {
            this.ecline_no_data = true
          }
        })
    },
    // 车间下拉改变
    change_Workshop(id) {
      this.Workshop_value = id
      this.ProductionLine_value = null
      this.ProductionLine = []
      this.getProductionLine()
    },
    // 获取车间下拉列表
    getWorkshop() {
      this.$axios
        .post(this.Eelec + "/workshop/getWorkshop", {
          custID: sessionStorage.getItem("custId")
        })
        .then(res => {
          if (res.data.code === 200) {
            if (res.data.data && res.data.data.length) {
              this.Workshop = res.data.data
              this.Workshop_value = res.data.data[0].id
              this.ProductionLine_Workshop_value += 1
              // 获取生产线下拉列表
              this.getProductionLine()
            } else {
              this.ecline_no_data = true
              this.ecline_loading = false
              this.table_loading = false
            }
          }
        })
    },
    // 获取生产线下拉列表
    getProductionLine() {
      this.$axios
        .post(this.Eelec + "/workshop/getProductionLine", {
          id: this.Workshop_value
        })
        .then(res => {
          if (res.data.code === 200) {
            if (res.data.data && res.data.data.length) {
              this.ProductionLine = res.data.data
              this.ProductionLine_value = res.data.data[0].id
              this.export_ProductionLine_value = res.data.data[0].id
              this.ProductionLine_Workshop_value += 1
            } else {
              this.ecline_no_data = true
              this.ecline_loading = false
              this.table_loading = false
            }
          }
        })
    },
    t_export() {
      // this.export_show = true
      this.$axios
        .post(this.Eelec + "/workshop/getUploadExcel", {
          downsample: this.export_value === 2 ? "month" : "year",
          equipID: null,
          id: this.export_ProductionLine_value,
          startTime: this.export_s_time,
          endTime: this.export_e_time
        })
        .then(res => {
          if (res.data.code === 200) {
            this.export_show = false
            if (res.data.data.path) {
              window.location.href = res.data.data.path
              this.$message({
                message: "导出成功",
                type: "success"
              })
            } else {
            }
          } else if (res.data.code === 1002) {
            this.export_show = false
            this.$message({
              message: res.data.msg,
              type: "error"
            })
          }
        })
    },
    // 点击查询
    t_button() {
      day
        .s_e(this.s_time, this.e_time, this.t_value)
        .then(() => {
          this.export_s_time = this.s_time
          this.export_e_time = this.e_time
          this.export_value = this.t_value
          this.export_ProductionLine_value = this.ProductionLine_value
          console.log("可以调方法了")
          this.table_loading = true
          this.ecline_loading = true
          // 车间用电统计表格
          this.getExcel()
          // 车间用电统计查询曲线
          this.getElectricity()
        })
        .catch(() => { })
    },
    // 切换年月日
    t_change(n) {
      if (n === 2) {
        this.content = '查询时间跨度最大为31天'
      }
      if (n === 3) {
        this.content = '查询时间跨度最大为12个月'
      }
      this.v_type = day.change_type(n).v
      this.d_type = day.change_type(n).d
      this.s_e = day.init_value(this.d_type)
      this.s_time = this.s_e.s
      this.e_time = this.s_e.e
    }
  }
}
</script>
 <style scoped lang='scss'>
</style>
